import { Component, OnInit } from '@angular/core';
import { Router } from '@angular/router';
import { MyModalHelper } from '@core/my-modal.helper';
import { MarketAgentService } from '@core/routes-services/market-agent.service';
import { NzMessageService } from 'ng-zorro-antd';
import { DistrictService } from '@core/routes-services/district.service';
import { SalesmanService } from '@core/routes-services/salesman.service';
import { MarketAgentLevelService } from '@core/routes-services/market-agent-level.service';
import { CustomerOriginLinkComponent } from '../../customer-origin-link/customer-origin-link.component';

@Component({
  selector: 'app-market-agent-sign',
  templateUrl: './sign.component.html',
})
export class MarketAgentSignComponent implements OnInit {


  isLoading = false;
  keyword: any;
  districtId: any;
  salesmanId: any;
  marketLevelId: any;
  page: any = { number: 1, size: 10, totalElements: 0, content: [] };

  allMarketAgentLevels:any [] = [];
  allDistricts:any [] = [];
  allSalesmans:any [] = [];

  constructor(private message: NzMessageService,
              private router: Router,
              private marketAgentService: MarketAgentService,
              private marketAgentLevelService:MarketAgentLevelService,
              private districtService:DistrictService,
              private salesmanService:SalesmanService,
              private modalHelper: MyModalHelper) {
  }

  ngOnInit() {
    this.initOptions();
    this.initPage();
  }


  //优化： 后续统一到一个接口中
  initOptions(){
    this.districtService.getAll().then((res) => {
      if (res.status.toString().startsWith("20")) {
        this.allDistricts = res.data;
      } else {
        this.message.warning(res.message);
      }
    }, error => {
      this.message.error(error.message ? error.message : error.error)
    });
    this.marketAgentLevelService.getAll().then((res) => {
      if (res.status.toString().startsWith("20")) {
        this.allMarketAgentLevels = res.data.marketAgentLevelsList;
      } else {
        this.message.warning(res.message);
      }
    }, error => {
      this.message.error(error.message ? error.message : error.error)
    });
    this.salesmanService.getSalesman().then((res) => {
      if (res.status.toString().startsWith("20")) {
        this.allSalesmans = res.data.marketSalesmanList;
      } else {
        this.message.warning(res.message);
      }
    }, error => {
      this.message.error(error.message ? error.message : error.error)
    });
  }


  initPage() {
    this.isLoading = true;
    this.marketAgentService.signPagination(this.page, this.keyword,this.districtId,this.salesmanId,this.marketLevelId).then((res) => {
      if (res.status.toString().startsWith('20')) {
        this.page = res.data;
        ++this.page.number;
      } else {
        this.message.warning(res.message);
      }
      this.isLoading = false;
    }, error => {
      this.message.error(error.message ? error.message : error.error);
      this.isLoading = false;
    });
  }

  /**
   * 搜索重置分页数据
   */
  search() {
    this.page = { number: 0, size: 10, totalElements: 0, content: [] };
    this.initPage();
  }

  changePageNo(pageNo) {
    this.page = { number: pageNo, size: 10, totalElements: 0, content: [] };
    this.initPage();
  }

  delete = (data) => {
    this.marketAgentService.deleteEntity(data.id).then((data) => {
      this.search();
      this.message.success('删除成功');
    }, error => console.log('删除出错，后续处理'));
  };

  seeLink= (entity) => {
    this.modalHelper.open(CustomerOriginLinkComponent, { customerId:entity.customerId }, 680, {
      nzTitle: '用户推广链',
    }).subscribe(result => {

    }, (error) => {
      console.error('弹窗回调出错：', error);
    });
  }

}
